using System;
using System.Collections;


namespace SharpWars.Core.AI
{
    class SortedCostNodeList
    {
        ArrayList List;
        NodeComparer NComparer;

        public int Count
        {
            get 
            {
                return List.Count;
            }
        }

        public SortedCostNodeList()
        {
            List = new ArrayList();
            NComparer = new NodeComparer();
        }

        public Node NodeAt(int i)
        {
            return (Node)List[i];
        }

        public void RemoveAt(int i)
        {
            List.Remove(i);
        }

        public int IndexOf(Node n)
        {
            for (int i = 0; i < List.Count; i++)
            {
                Node temp = (Node)List[i];
                if (temp.IsMatch(n))
                    return i;
            }
            return -1;
        }
        public int Push(Node n)
        {

            int k = List.BinarySearch(n, NComparer);

            if (k == -1) // no element
                List.Insert(0, n);
            else if (k < 0) // find location by complement
            {
                k = ~k;
                List.Insert(k, n);
            }
            else if (k >= 0)
                List.Insert(k, n);

            return k;
        }

        public Node Pop()
        {
            Node r = (Node)List[0];
            List.RemoveAt(0);
            return r;
        }
    }
}
